package com.thkj.common.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class Singleton {
	
	private static Log logger = LogFactory.getLog(Singleton.class);
	// 缓存accessToken 的Map,map中包含 一个accessToken 和 缓存的时间戳
	private Map<String, String> accessTokenMap = new HashMap<String, String>();
	private Map<String, String> jsapiTicketMap = new HashMap<String, String>();

	private Singleton() {

	}

	private static Singleton single = null;

	// 静态工厂方法
	public static Singleton getInstance() {
		if (single == null) {
			single = new Singleton();
		}
		return single;
	}

	public Map<String, String> getAccessTokenMap() {
		return accessTokenMap;
	}

	public void setAccessTokenMap(Map<String, String> accessTokenMap) {
		this.accessTokenMap = accessTokenMap;
	}

	public Map<String, String> getJsapiTicketMap() {
		return jsapiTicketMap;
	}

	public void setJsapiTicketMap(Map<String, String> jsapiTicketMap) {
		this.jsapiTicketMap = jsapiTicketMap;
	}

	public static Singleton getSingle() {
		return single;
	}

	public static void setSingle(Singleton single) {
		Singleton.single = single;
	}

	public synchronized String getAccessToken(String appid, String appsecret) {
		Singleton singleton = Singleton.getInstance();
		Map<String, String> map = singleton.getAccessTokenMap();
		String time = map.get("time");// 从缓存中拿数据
		String accessToken = map.get("access_token");// 从缓存中拿数据
		Long nowDate = new Date().getTime();
		// 这里设置过期时间 3000*1000就好了
		if (accessToken != null && time != null && nowDate - Long.parseLong(time) < 3000 * 1000) {
			logger.info("从缓存读取access_token==>" + accessToken);
		} else {
			accessToken = makeAccessToken(appid, appsecret);
			// 将信息放置缓存中
			map.put("time", nowDate + "");
			map.put("access_token", accessToken);
			logger.info("生成access_token==>" + accessToken);
		}
		return accessToken;
	}
	
	public synchronized String getJsapiTicket() {
		Singleton singleton = Singleton.getInstance();
		Map<String, String> map = singleton.getAccessTokenMap();
		String time = map.get("time");// 从缓存中拿数据
		String jsapiTicket = map.get("jsapiticket");// 从缓存中拿数据
		Long nowDate = new Date().getTime();
		// 这里设置过期时间 3000*1000就好了
		if (jsapiTicket != null && time != null && nowDate - Long.parseLong(time) < 3000 * 1000) {
			logger.info("从缓存读取jsapiTicket==>" + jsapiTicket);
		} else {
			String accessToken = getAccessToken(OAuthUtil.APPID, OAuthUtil.APPSECRET);
			jsapiTicket = makeJsapiTicket(accessToken);
			// 将信息放置缓存中
			map.put("time", nowDate + "");
			map.put("jsapiticket", jsapiTicket);
			logger.info("生成jsapiTicket==>" + jsapiTicket);
		}
		return jsapiTicket;
	}
	
	private String makeAccessToken(String appid, String appsecret) {
		String accessToken = null;
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret;
		HttpClient httpClient = new HttpClient();
		PostMethod post = new PostMethod(url);
		try {
			httpClient.executeMethod(post);
			String jsonStr = post.getResponseBodyAsString();
			JSONObject json = JSON.parseObject(jsonStr);
			if (json.get("access_token") != null) {
				accessToken = json.getString("access_token");
			}else {
				System.out.println(jsonStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			post.releaseConnection();// 释放连接
		}
		return accessToken;
	}
	
	private String makeJsapiTicket(String accessToken) {
		String jsapiTicket = null;
		String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+accessToken+"&type=jsapi";
		HttpClient httpClient = new HttpClient();
		PostMethod post = new PostMethod(url);
		try {
			httpClient.executeMethod(post);
			String jsonStr = post.getResponseBodyAsString();
			JSONObject json = JSON.parseObject(jsonStr);
			if (json.get("ticket") != null) {
				jsapiTicket = json.getString("ticket");
			}else {
				System.out.println(jsonStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			post.releaseConnection();// 释放连接
		}
		return jsapiTicket;
	}
}
